<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Admin\Controller;
use User\Api\UserApi;
use Think\Model;
use Org\Util;

/**
 * 后台首页控制器
 * @author 麦当苗儿 <zuojiazi@vip.qq.com>
 */
class AppmanagementController extends AdminController {

    public function index(){
        if(UID){
            $this->meta_title = '应用管理';
            $ae = new Util\AEHttp("api");
            $result = $ae->find("api_app","delflag = 0");
            $list = array_map(function($d){
              return json_decode(json_encode($d),true);
            },$result->data);
            $this->assign('_list', $list);
            $this->display();
        } else {
            $this->redirect('Public/login');
        }
    }

    //添加应用
    public function add(){
        if(IS_POST){
            $data = get_form_fields(array('appname','apptype','appenvironment','approot','about','appurl'));
            //转换一下权限的变量 [] -> string
            $data['approot'] = join(',',$data['approot']);
            //生成 appkey & masterkey 和应用标识
            $uuid = create_guid();
            $data['appid'] = time();
            $data['appkey'] = substr(md5($uuid),8,16);
            $data['secretkey'] = md5($uuid.'CONST_SECURITY_VALUE'.time());
            $data['createAt'] = $data['updateAt'] = time();
            //保存信息
            $ae = new Util\AEHttp("api");
            $result = $ae->create('api_app',$data);
            if($result->errno === 0){
              //ok
              $this->success('创建成功！', U('index'));
            }else {
              $this->error("创建失败！可能是：".$result->message);
            }
        }else{
            $this->display();
        }
    }

    //预览应用
    public function review(){
        $id = I('id');
        if(!$id){
          $this->error("您要预览的应用ID未指定!");
        }else{
          //fetch the app info
          $ae = new Util\AEHttp("api");
          $result = $ae->get('api_app',$id);
          if($result->errno === 0){
            $this->assign('app',json_decode(json_encode($result->data),true));
            $this->display();
          }else{
            $this->error("操作错误,可能的错误是:".$result->message);
          }
        }
    }

    public function approve(){
      $id = I('id');
      if(!$id){
        $this->error("您要审核的应用ID未指定!");
      }else{
        //fetch the app info
        $ae = new Util\AEHttp("api");
        $result = $ae->update('api_app','id = '.$id,array('status' => 0));
        if($result->errno === 0){
          $this->success('审核成功！', U('index'));
        }else{
          $this->error("操作错误,可能的错误是:".$result->message);
        }
      }
    }

    public function reject(){
      $id = I('id');
      if(!$id){
        $this->error("您要驳回的应用ID未指定!");
      }else{
        //fetch the app info
        $ae = new Util\AEHttp("api");
        $result = $ae->update('api_app','id = '.$id,array('status' => 2));
        if($result->errno === 0){
          $this->success('已驳回！', U('index'));
        }else{
          $this->error("操作错误,可能的错误是:".$result->message);
        }
      }
    }

    //删除应用
    public function del(){
        $id = I('id');
        if(!$id){
          $this->error("您要删除的应用ID未指定!");
        } else {
          $ae = new Util\AEHttp("api");
          $ae->remove("api_app",$id);
          $this->success('删除成功！', U('index'));
        }
    }

}
